from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select


class BasePage:
    def __init__(self, driver=None):
        if driver is not None:
            self.driver = driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)
            self.driver.maximize_window()
# 实现 元素定位方式的 分割  "id , su"
    def converter(self, target):
        id, value = target.split(",")
        return id.strip(), value.strip()
    #    (XPATH "/ID=SU")
    def locate_element(self, target):
        id, value = self.converter(target)
        return self.driver.find_element(id, value)
                    # "id, su" , "admin"

    def input(self, target, keyvalue):
        self.locate_element( target).send_keys(keyvalue)

    def click(self, target):
        self.locate_element( target).click()

    def open(self, url, timeout=5):
        self.driver.get(url)

    def get_ele_text(self, target):
        return self.locate_element( target).text

    def get_attribute(self, target, param):
        return self.locate_element( target).get_attribute(param)

    def into_frame(self, frame_name):
        self.driver.switch_to.frame(frame_name)

    def dropdown(self, target, num):
        sle = self.locate_element(target)
        sle_obj = Select(sle)
        sle_obj.select_by_index(num)

    def quit(self):
        self.driver.quit()

    def wait(self, seconds):
        sleep(seconds)

if __name__ == '__main__':
    bg = BasePage()
    bg.open("http://192.168.1.138/ranzhi/www/sys/index.html")
    bg.wait(1)
    bg.input("id,account","admin")
    bg.wait(1)
    bg.input("id,password","123456")
    bg.wait(1)
    bg.click("id,submit")
    bg.wait(5)